package cn.kgc.easybuy.controller;

import cn.kgc.easybuy.annotaion.CheckPermission;
import cn.kgc.easybuy.pojo.Category;
import cn.kgc.easybuy.service.CategoryService;
import cn.kgc.easybuy.utils.AjaxResult;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: yingzi-wants-to-sleep
 * @description:
 * @author: 敖成
 * @create: 2025-09-11 14:47
 **/
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/selectCategoryById")
    public AjaxResult selectCategoryById(int id) {
        return AjaxResult.success().add("category", categoryService.selectCategoryById(id));
    }
    @GetMapping("/selectCategoryByType")
    public AjaxResult selectCategoryByType(int type) {
        return AjaxResult.success().add("categoryByTypeList", categoryService.selectCategoryByType(type));
    }

    @GetMapping("/selectAllCategory")
    public AjaxResult selectAllCategory() {
        return AjaxResult.success().add("categoryList", categoryService.selectAllCategory());
    }

    @GetMapping("/selectRootCategories")
    public AjaxResult selectRootCategories() {
        return  AjaxResult.success().add("allCategoryWithChild",categoryService.selectRootCategories());
    }

    @PostMapping("/selectCategoryByPage")
    public AjaxResult selectCategoryByPage(@RequestBody Map<String, Object> params) {
        return AjaxResult.success().add("pageInfo", categoryService.selectCategoryByPage(params));
    }

    @GetMapping("/selectChildCategory")
    public AjaxResult selectChildCategory(int parentId) {
        return AjaxResult.success().add("childCategoryList", categoryService.selectChildCategory(parentId));
    }

    @CheckPermission({"0","1"})
    @PostMapping("/addCategory")
    public AjaxResult addCategory(@RequestBody Category category) {
        boolean isAdd = categoryService.addCategory(category);
        if (isAdd){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PostMapping("/updateCategory")
    public AjaxResult updateCategory(@RequestBody Category category) {
        boolean isUpdate = categoryService.updateCategory(category);
        if (isUpdate){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/delCategory")
    public AjaxResult delCategory(int id) {

        boolean isDel = categoryService.delCategory(id);
        if (isDel){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }

    }

    @GetMapping("/checkName")
    public AjaxResult checkName(@Param("name") String name, @Param("type") Integer type) {
        boolean isCheck = categoryService.checkName(name,type);
        if (isCheck){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}
